$(document).ready(function(){
	
	prettyPrint();			//syntax highlighter
	mainwrapperHeight();
	responsive();
	
	
	// animation
	if($('.contentinner').hasClass('content-dashboard')) {
		var anicount = 1;	
		$('.sidebar .nav-side > li').each(function(){										   
			$(this).addClass('animate'+anicount+' fadeInUp');
			anicount++;
		});
		
		$('.sidebar .nav-side > li a').hover(function(){
			$(this).find('i').addClass('animate0 wobble');
		},function(){
			$(this).find('i').removeClass('animate0 wobble');
		});
		
		
		$('.alert').addClass('animate0 fadeInUp');
		$('.widgeticons').addClass('animate1 fadeInUp');
		$('.widgettitle, .widgetcontent, .contentinner table').addClass('animate2 fadeInUp'); 
		$('.plainwidget, .pagetitle').addClass('animate3 fadeInUp');
		$('.maincontent').addClass('animate4 fadeInUp');
	}
	
	// widget icons dashboard
	if($('.widgeticons').length > 0) {
		$('.widgeticons a').hover(function(){
			$(this).find('img').addClass('animate0 bounceIn');
		},function(){
			$(this).find('img').removeClass('animate0 bounceIn');
		});	
	}


	// adjust height of mainwrapper when 
	// it's below the document height
	function mainwrapperHeight() {
		var windowHeight = $(window).height();
		var mainWrapperHeight = $('.mainwrapper').height();
		var leftPanelHeight = $('.leftpanel').height();
		if(leftPanelHeight > mainWrapperHeight)
			$('.mainwrapper').css({minHeight: leftPanelHeight});	
		if($('.mainwrapper').height() < windowHeight)
			$('.mainwrapper').css({minHeight: windowHeight});
	}
	
	function responsive() {
		
		var windowWidth = $(window).width();
		
		// hiding and showing left menu
		if(!$('.showmenu').hasClass('clicked')) {
			
			if(windowWidth < 960)
				hideLeftPanel();
			else
				showLeftPanel();
		}
		
		// rearranging widget icons in dashboard
		if(windowWidth < 768) {
			if($('.widgeticons .one_third').length == 0) {
				var count = 0;
				$('.widgeticons li').each(function(){
					$(this).removeClass('one_fifth last').addClass('one_third');
					if(count == 2) {
						$(this).addClass('last');
						count = 0;
					} else { count++; }
				});	
			}
		} else {
			if($('.widgeticons .one_firth').length == 0) {
				var count = 0;
				$('.widgeticons li').each(function(){
					$(this).removeClass('one_third last').addClass('one_fifth');
					if(count == 4) {
						$(this).addClass('last');
						count = 0;
					} else { count++; }
				});	
			}
		}
	}
	
	// when resize window event fired
	$(window).resize(function(){
		mainwrapperHeight();
		responsive();
	});
	
	// dropdown in leftmenu
	$('.leftmenu .dropdown > a').click(function(){
		if(!$(this).next().is(':visible'))
			$(this).next().slideDown('fast');
		else
			$(this).next().slideUp('fast');	
		return false;
	});
	
	// hide left panel
	function hideLeftPanel() {
		$('.sidebar').css({marginLeft: '-230px'}).addClass('hide');
		$('.main-content').css({marginLeft: 0});
		$('.contentinner').css({backgroundPosition: '-230px 0'});
		$('.footerleft').hide();
		$('.footerright').css({marginLeft: 0});
	}
	
	// show left panel
	function showLeftPanel() {
		$('.sidebar').css({marginLeft: '0px'}).removeClass('hide');
		$('.main-content').css({marginLeft: '230px'});
		$('.contentinner').css({backgroundPosition: '0 0'});
		$('.footerleft').show();
		$('.footerright').css({marginLeft: '230px'});
	}
	
	// show and hide left panel
	$('.showmenu').click(function() {
		if($("#main-sidebar").is(":hidden")) {
			$('#main-sidebar').show();
			$('.main-content').css({marginLeft: '230px'});
		} else {
			$('#main-sidebar').hide();
			$('.main-content').css({marginLeft: '0px'});
		}
		return false;
	});
	
	// transform checkbox and radio box using uniform plugin
	if($().uniform)
		$('input:checkbox, input:radio, select.uniformselect').uniform();
	
	
	// 显示隐藏源码
	if($('.showhide').length > 0 ) {
		$('.showhide').click(function(){
			var t = $(this);
			var p = t.parent();
			var target = t.attr('href');
			target = (!target)? p.next() :	p.next().find('.'+target);
			t.text((target.is(':visible'))? '查看源码' : '隐藏源码');
			(target.is(':visible'))? target.hide() : target.show(100);
			return false;
		});
	}
	
	
	// 表格全选
	if($('.checkall').length > 0) {
		$('.checkall').click(function(){
			var parentTable = $(this).parents('table');										   
			var ch = parentTable.find('tbody input[type=checkbox]');										 
			if($(this).is(':checked')) {
			
				//check all rows in table
				ch.each(function(){ 
					$(this).attr('checked',true);
					$(this).parent().addClass('checked');	//used for the custom checkbox style
					$(this).parents('tr').addClass('selected'); // to highlight row as selected
				});
							
			
			} else {
				
				//uncheck all rows in table
				ch.each(function(){ 
					$(this).attr('checked',false); 
					$(this).parent().removeClass('checked');	//used for the custom checkbox style
					$(this).parents('tr').removeClass('selected');
				});	
				
			}
		});
	}
	
	
	// 表格删除
	if($('.deleterow').length > 0) {
		$('.deleterow').click(function(){
			var conf = confirm('Continue delete?');
			if(conf)
				$(this).parents('tr').fadeOut(function(){
					$(this).remove();
					// do some other stuff here
				});
			return false;
		});	
	}
	
	
	// 动态表格
	if($('#dyntable').length > 0) {
		$('#dyntable').dataTable({
			"sPaginationType": "full_numbers",
			"aaSortingFixed": [[0,'asc']],
			"fnDrawCallback": function(oSettings) {
				$.uniform.update();
			}
		});
	}
	
	
	/////////////////////////////// ELEMENTS.HTML //////////////////////////////
	
	
	// tabbed widget
	$('#tabs, #tabs2').tabs();
	
	// accordion widget
	$('#accordion, #accordion2').accordion({heightStyle: "content"});
	
	
	// color picker
	if($('#colorpicker').length > 0) {
		$('#colorSelector').ColorPicker({
			onShow: function (colpkr) {
				$(colpkr).fadeIn(500);
				return false;
			},
			onHide: function (colpkr) {
				$(colpkr).fadeOut(500);
				return false;
			},
			onChange: function (hsb, hex, rgb) {
				$('#colorSelector span').css('backgroundColor', '#' + hex);
				$('#colorpicker').val('#'+hex);
			}
		});
	}

	
	// date picker
	if($('#datepicker').length > 0)
		$( "#datepicker" ).datepicker();
		
	
	// another growl notification
	if($('#growl').length > 0) {
		$('#growl').click(function(){
			var msg = "弹出提示1弹出提示1弹出提示1!";
			$.jGrowl(msg, { life: 5000});
		});
	}
	
	// another growl notification
	if($('#growl2').length > 0) {
		$('#growl2').click(function(){
			var msg = "弹出提示2弹出提示2弹出提示2!";
			$.jGrowl(msg, { life: 5000});
		});
	}
	
	// another growl notification
	if($('#growl3').length > 0) {
		$('#growl3').click(function(){
			var msg = "弹出提示3弹出提示3弹出提示3!";
			$.jGrowl(msg, { life: 5000});
		});
	}

	// basic alert box
	if($('.alertboxbutton').length > 0) {
		$('.alertboxbutton').click(function(){
			jAlert('警示框内容警示框内容', '警示框标题');
		});
	}
	
	// confirm box
	if($('.alertboxbutton2').length > 0) {
		$('.alertboxbutton2').click(function(){
			jConfirm('提示框问题？', '警示框标题', function(r) {
				jAlert('答案: ' + r, '确认结果');
			});
		});
	}
	
	// promptbox
	if($('.alertboxbutton3').length > 0) {
		$('.alertboxbutton3').click
		(function(){
			jPrompt('请输入:', '三星9008', '警示框标题', function(r) {
				if( r ) alert('输入信息 ' + r);
			});
		});
	}
	
	// alert with html
	if($('.alertboxbutton4').length > 0) {
		$('.alertboxbutton4').click(function(){
			jAlert('使用Html标签, 比如 <strong>粗体</strong>, <em>斜体</em>, 还有 <u>下划线</u>!', '警示框标题');
		});
	}
	
	// sortable list
	if($('#sortable').length > 0)
		$("#sortable").sortable();
	
	// sortable list with content-->
	if($('#sortable2').length > 0) {
		$("#sortable2").sortable();
		$('.showcnt').click(function(){
			var t = $(this);
			var det = t.parents('li').find('.details');
			if(!det.is(':visible')) {
				det.slideDown();
				t.removeClass('icon-arrow-down').addClass('icon-arrow-up');
			} else {
				det.slideUp();
				t.removeClass('icon-arrow-up').addClass('icon-arrow-down');
			}
		});
	}
	
	// tooltip sample
	if($('.tooltipsample').length > 0)
		$('.tooltipsample').tooltip({selector: "a[rel=tooltip]"});
		
	$('.popoversample').popover({selector: 'a[rel=popover]', trigger: 'hover'});
	
	
	
	///// MESSAGES /////	
	
	if($('.mailinbox').length > 0) {
		
		// star
		$('.msgstar').click(function(){
			if($(this).hasClass('starred'))
				$(this).removeClass('starred');
			else
				$(this).addClass('starred');
		});
		
		//add class selected to table row when checked
		$('.mailinbox tbody input:checkbox').click(function(){
			if($(this).is(':checked'))
				$(this).parents('tr').addClass('selected');
			else
				$(this).parents('tr').removeClass('selected');
		});
		
		// trash
		if($('.msgtrash').length > 0) {
			$('.msgtrash').click(function(){
				var c = false;
				var cn = 0;
				var o = new Array();
				$('.mailinbox input:checkbox').each(function(){
					if($(this).is(':checked')) {
						c = true;
						o[cn] = $(this);
						cn++;
					}
				});
				if(!c) {
					alert('No selected message');	
				} else {
					var msg = (o.length > 1)? 'messages' : 'message';
					if(confirm('Delete '+o.length+' '+msg+'?')) {
						for(var a=0;a<cn;a++) {
							$(o[a]).parents('tr').remove();	
						}
					}
				}
			});
		}
	}

	
	// change layout
	$('.skin-layout').click(function(){
		$('.skin-layout').each(function(){ $(this).parent().removeClass('selected'); });
		if($(this).hasClass('fixed')) {
			$('.mainwrapper').removeClass('fullwrapper');
			if($('.stickyheaderinner').length > 0) $('.stickyheaderinner').removeClass('wideheader');
			$.cookie("skin-layout", 'fixed', { path: '/' });
		} else {
			$('.mainwrapper').addClass('fullwrapper');
			if($('.stickyheaderinner').length > 0) $('.stickyheaderinner').addClass('wideheader');
			$.cookie("skin-layout", 'wide', { path: '/' });
		}
		return false;
	});
	
	// load selected layout from cookie
	if($.cookie('skin-layout')) {
		var layout = $.cookie('skin-layout');
		if(layout == 'fixed') {
			$('.mainwrapper').removeClass('fullwrapper');
			if($('.stickyheaderinner').length > 0) $('.stickyheaderinner').removeClass('wideheader');
		} else {
			$('.mainwrapper').addClass('fullwrapper');
			if($('.stickyheaderinner').length > 0) $('.stickyheaderinner').addClass('wideheader');
		}	
	}
	
	
	// change skin color
	$('.skin-color').click(function(){
		var s = $(this).attr('href');
		if($('#skinstyle').length > 0) {
			if(s!='default') {
				$('#skinstyle').attr('href','css/style.'+s+'.css');	
				$.cookie('skin-color', s, { path: '/' });
			} else {
				$('#skinstyle').remove();
				$.cookie("skin-color", '', { path: '/' });
			}
		} else {
			if(s!='default') {
				$('head').append('<link id="skinstyle" rel="stylesheet" href="css/style.'+s+'.css" type="text/css" />');
				$.cookie("skin-color", s, { path: '/' });
			}
		}
		return false;
	});
	
	// load selected skin color from cookie
	if($.cookie('skin-color')) {
		var c = $.cookie('skin-color');
		if(c) {
			$('head').append('<link id="skinstyle" rel="stylesheet" href="css/style.'+c+'.css" type="text/css" />');
			$.cookie("skin-color", c, { path: '/' });
		}
	}
	
});